package leetcode_700;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *@author 周杨
 *EmployeeImportance_690 寻找员工及其所管辖员工的重要性总和
 *describe:用深搜 AC 51%
 *2018年10月8日 下午2:55:38
 */
public class EmployeeImportance_690 {
	class Employee {
	    // It's the unique id of each node;
	    // unique id of this employee
	    public int id;
	    // the importance value of this employee
	    public int importance;
	    // the id of direct subordinates
	    public List<Integer> subordinates;
	}
	
	public int getImportance(List<Employee> employees, int id) {
        Map<Integer,Employee> map=new HashMap<Integer,Employee>();
        for(Employee employee:employees) {
        	map.put(employee.id, employee);
        }
        return help(map,id);
    }

	private int help(Map<Integer, Employee> map, int id) {
		int res=0;
		Employee employee=map.get(id);
		res+=employee.importance;
		if(employee.subordinates!=null) {
			for(int id_:employee.subordinates) {
				res+=help(map,id_);
			}
		}
		return res;
	}
}
